home *** CD-ROM | disk | FTP | other *** search
/ All for Cell Phones: Sony Ericsson / Sony-Ericsson 2004.iso / Java / Excel / Sheet.jar / sheet / CellCalc.class (.txt) < prev    next >
Encoding:
Java Class File  |  2001-09-03  |  4.7 KB  |  254 lines

  1. package sheet;
  2.  
  3. import java.util.EmptyStackException;
  4. import java.util.Stack;
  5.  
  6. public final class CellCalc {
  7.    private static final short STACK_DEPTH = 10;
  8.    private static final short SYM_DATA = 1;
  9.    private static final short SYM_OPEN_BR = 2;
  10.    private static final short SYM_CLOSE_BR = 3;
  11.    private static final short SYM_DELIMITER = 4;
  12.    private static final short SYM_OPERATION = 5;
  13.    private static final short SYM_STRING = 6;
  14.    private static final short SYM_COMMA = 7;
  15.    private static final short PUSH_NO = 0;
  16.    private static final short PUSH_OPERATION = 1;
  17.    private static final short PUSH_DATA = 2;
  18.    private static final short PUSH_FUNCTION = 3;
  19.    private static final Operation[] OPS = new Operation[]{new Operation((short)0, (short)0, 4, '+'), new Operation((short)1, (short)0, 4, '-'), new Operation((short)2, (short)2, 2, '+'), new Operation((short)3, (short)2, 2, '-'), new Operation((short)4, (short)2, 3, '*'), new Operation((short)5, (short)2, 3, '/'), new Operation((short)6, (short)3, 5, '\u0000')};
  20.    private Calculator callBack;
  21.    private IntegerStack operations = new IntegerStack((1)null);
  22.    private Stack operands = new Stack();
  23.    private String expression;
  24.    private short last_push;
  25.    private short fargs;
  26.  
  27.    public CellCalc(Calculator var1) {
  28.       this.callBack = var1;
  29.    }
  30.  
  31.    private static short symbolType(char var0) {
  32.       switch (var0) {
  33.          case '\t':
  34.          case '\n':
  35.          case '\r':
  36.          case ' ':
  37.             return 4;
  38.          case '\u000b':
  39.          case '\f':
  40.          case '\u000e':
  41.          case '\u000f':
  42.          case '\u0010':
  43.          case '\u0011':
  44.          case '\u0012':
  45.          case '\u0013':
  46.          case '\u0014':
  47.          case '\u0015':
  48.          case '\u0016':
  49.          case '\u0017':
  50.          case '\u0018':
  51.          case '\u0019':
  52.          case '\u001a':
  53.          case '\u001b':
  54.          case '\u001c':
  55.          case '\u001d':
  56.          case '\u001e':
  57.          case '\u001f':
  58.          case '!':
  59.          case '#':
  60.          case '$':
  61.          case '%':
  62.          case '&':
  63.          case '\'':
  64.          case '.':
  65.          default:
  66.             return 1;
  67.          case '"':
  68.             return 6;
  69.          case '(':
  70.             return 2;
  71.          case ')':
  72.             return 3;
  73.          case '*':
  74.          case '+':
  75.          case '-':
  76.          case '/':
  77.             return 5;
  78.          case ',':
  79.             return 7;
  80.       }
  81.    }
  82.  
  83.    private boolean execute(boolean var1, int var2) throws RuntimeException {
  84.       short var3;
  85.       try {
  86.          var3 = this.operations.pop();
  87.       } catch (EmptyStackException var10) {
  88.          return false;
  89.       }
  90.  
  91.       if (var3 == 7) {
  92.          if (var1) {
  93.             this.operations.push(var3);
  94.          }
  95.  
  96.          return false;
  97.       } else if (var2 > OPS[var3].priority) {
  98.          this.operations.push(var3);
  99.          return false;
  100.       } else {
  101.          switch (OPS[var3].type) {
  102.             case 2:
  103.                try {
  104.                   Operand var5 = (Operand)this.operands.pop();
  105.                   Operand var4 = (Operand)this.operands.pop();
  106.                   this.operands.push(this.callBack.oper(var3, var4, var5));
  107.                   break;
  108.                } catch (IllegalArgumentException var8) {
  109.                   throw var8;
  110.                } catch (RuntimeException var9) {
  111.                   throw new IllegalArgumentException(Operation.OPNAME[var3] + " ops are wrong");
  112.                }
  113.             case 3:
  114.                Operand[] var6 = new Operand[this.fargs];
  115.  
  116.                for(short var7 = 0; var7 < this.fargs; ++var7) {
  117.                   var6[this.fargs - var7 - 1] = (Operand)this.operands.pop();
  118.                }
  119.  
  120.                this.fargs = 0;
  121.                this.operands.push(this.callBack.call(Operand.getName((Operand)this.operands.pop()), var6));
  122.                break;
  123.             default:
  124.                this.operands.push(this.callBack.oper(var3, (Operand)this.operands.pop(), (Operand)null));
  125.          }
  126.  
  127.          return true;
  128.       }
  129.    }
  130.  
  131.    private short pushOperand(short var1, short var2) throws IllegalArgumentException {
  132.       if (var1 != var2) {
  133.          Object var3 = null;
  134.          if (this.last_push == 3) {
  135.             Operand var4 = (new CellCalc(this.callBack)).calculate(this.expression, var1, var2);
  136.             this.operands.push(var4);
  137.             ++this.fargs;
  138.          } else {
  139.             Operand var5 = new Operand(this.expression.substring(var1, var2));
  140.             this.operands.push(var5);
  141.             this.last_push = 2;
  142.          }
  143.       }
  144.  
  145.       return (short)(var2 + 1);
  146.    }
  147.  
  148.    public Operand calculate(String var1, short var2, short var3) throws IllegalArgumentException {
  149.       short var4 = var2;
  150.       short var5 = var2;
  151.       short var6 = 0;
  152.  
  153.       while(!this.operands.empty()) {
  154.          this.operands.pop();
  155.       }
  156.  
  157.       this.operations.clear();
  158.       this.last_push = 0;
  159.  
  160.       for(this.expression = var1; var4 < var3; ++var4) {
  161.          char var7 = var1.charAt(var4);
  162.          switch (symbolType(var7)) {
  163.             case 2:
  164.                if (this.last_push == 3) {
  165.                   ++var6;
  166.                } else {
  167.                   var5 = this.pushOperand(var5, var4);
  168.                   if (this.last_push == 2) {
  169.                      this.operations.push((short)6);
  170.                      this.last_push = 3;
  171.                      this.fargs = 0;
  172.                      var6 = 1;
  173.                   } else {
  174.                      this.operations.push((short)7);
  175.                      this.last_push = 1;
  176.                   }
  177.                }
  178.                break;
  179.             case 3:
  180.                if (this.last_push == 3) {
  181.                   --var6;
  182.                }
  183.  
  184.                if (var6 == 0) {
  185.                   for(var5 = this.pushOperand(var5, var4); this.execute(false, 0); this.last_push = 2) {
  186.                   }
  187.                }
  188.                break label4;
  189.             case 4:
  190.                if (this.last_push != 3) {
  191.                   var5 = this.pushOperand(var5, var4);
  192.                }
  193.                break;
  194.             case 5:
  195.                if (this.last_push == 3) {
  196.                   break;
  197.                }
  198.  
  199.                var5 = this.pushOperand(var5, var4);
  200.                short var8 = Operation.find(OPS, var7, this.last_push != 2);
  201.  
  202.                try {
  203.                   while(this.execute(true, OPS[var8].priority)) {
  204.                      this.last_push = 2;
  205.                   }
  206.  
  207.                   this.operations.push(var8);
  208.                   this.last_push = 1;
  209.                   if (OPS[var8].type == 1) {
  210.                      while(this.execute(true, OPS[var8].priority)) {
  211.                         this.last_push = 2;
  212.                      }
  213.                   }
  214.                   break;
  215.                } catch (ArrayIndexOutOfBoundsException var10) {
  216.                   throw new IllegalArgumentException("Bad " + String.valueOf(var7));
  217.                }
  218.             case 6:
  219.                if (var4 != var5) {
  220.                   throw new IllegalArgumentException("\" inside text");
  221.                }
  222.  
  223.                do {
  224.                   ++var4;
  225.                } while(var4 < var3 && symbolType(var1.charAt(var4)) != 6);
  226.  
  227.                if (var4 >= var3) {
  228.                   throw new IllegalArgumentException("No ending \"");
  229.                }
  230.  
  231.                ++var4;
  232.                this.pushOperand(var5, var4);
  233.                var5 = var4--;
  234.                break;
  235.             case 7:
  236.                if (var6 == 1) {
  237.                   var5 = this.pushOperand(var5, var4);
  238.                }
  239.          }
  240.       }
  241.  
  242.       this.pushOperand(var5, var4);
  243.  
  244.       while(this.execute(true, 0)) {
  245.       }
  246.  
  247.       if (!this.operations.isEmpty()) {
  248.          throw new IllegalArgumentException(Operation.OPNAME[this.operations.pop()] + " doesn't have a match");
  249.       } else {
  250.          return this.callBack.oper((short)0, (Operand)this.operands.pop(), (Operand)null);
  251.       }
  252.    }
  253. }
  254.